package com.zy.teacherSide.controller;

import com.zy.teacherSide.entity.Classroom;
import com.zy.teacherSide.entity.ov.ClassroomOV;
import com.zy.teacherSide.service.IClassroomService;
import com.zy.util.R;
import com.zy.util.RandomNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 29787
 */
@RestController
public class ClassroomController {

    @Autowired
    private IClassroomService ics;

    /**
     * 教师创建班级
     * @param c
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherCreateClassroom/{teacherId}")
    public R insertClassroom(@RequestBody Classroom c,@PathVariable Integer teacherId){
        //校验该老师是否已经创建此班级
        if (ics.getClassroomByNameAndTeacherId(c.getClassroomName(),teacherId) == null){
            c.setTeacherId(teacherId);
            Integer id = RandomNum.getRandomID(6);
            c.setClassroomId(id);
            return new R(ics.save(c),"success");
        } else {
            return new R(false,"err");
        }
    }

    /**
     * 当前教师获取名下的所有班级信息
     * @param teacherId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherGetAllClassByTeacherId/{teacherId}")
    public R getAllClassByTeacherId(@PathVariable Integer teacherId){
        return new R(true,ics.getAllClassroom(teacherId));
    }

    /**
     * 教师编辑班级信息
     * @param c
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherUploadClassroom")
    public R uploadClassroom(@RequestBody Classroom c){
        return new R(ics.updateById(c));
    }

    /**
     * 教师删除班级信息
     * @param id
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherDeleteClassroom/{id}")
    public R deleteStudentInformation(@PathVariable Integer id){
        return new R(ics.removeById(id));
    }


    /**
     * 获取当前老师的所有班级的班级名称
     * @param teacherId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/getAllClassroomByTeacherId/{teacherId}")
    public R getAllClassroomByTeacherId(@PathVariable Integer teacherId) {
        List<Classroom> allClassroom = ics.getAllClassroom(teacherId);
        List<ClassroomOV> list = new ArrayList<ClassroomOV>();
        for (Classroom c : allClassroom) {
            ClassroomOV cov = new ClassroomOV();
            cov.setClassroomName(c.getClassroomName());
            list.add(cov);
        }
        return new R(true,list);
    }

    /**
     * 教师获取班级名称
     * @param classroomId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherGetClassroomByClassroomId/{classroomId}")
    public R getClassroomByClassroomId(@PathVariable Integer classroomId) {
        return new R(true,ics.getClassroomByClassroomId(classroomId));
    }


}
